package com.cne.gmail.Gmail.es;

import org.elasticsearch.index.query.*;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field;
import org.elasticsearch.search.sort.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("postService")
public class postServiceImpl implements PostService {

    @Autowired
    PostRepository postRepository;
    @Override
    public Page<EsEmail> search(String name,int page,int size) {
        /*使用中文拼音混合搜索，取分数最高的，具体评分规则可参照： //  https://blog.csdn.net/paditang/article/details/79098830 */
        //Sort sort=new Sort(Sort.Direction.DESC,"intnetdata");
        Pageable pageable=PageRequest.of(page,size);
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                                        .withQuery(structureQuery(name))
                                        .withSort(SortBuilders.fieldSort("intnetdate").order(SortOrder.DESC)/*new FieldSortBuilder("intnetdata").order(SortOrder.ASC)*/)
                                        .withPageable(pageable)
                                       // .withHighlightFields(new HighlightBuilder.Field("subject").fragmentOffset(15),new HighlightBuilder.Field("from").fragmentSize(15))
                                        .build();
        Page<EsEmail> search = postRepository.search(searchQuery);
        return search;
    }
    public DisMaxQueryBuilder structureQuery(String content) {
        DisMaxQueryBuilder disMaxQueryBuilder = QueryBuilders.disMaxQuery();
        MatchPhraseQueryBuilder title = QueryBuilders.matchPhraseQuery("emailAddress", content);
        MatchPhraseQueryBuilder userName = QueryBuilders.matchPhraseQuery("userName", content);
        MatchPhraseQueryBuilder To = QueryBuilders.matchPhraseQuery("To", content);
        MatchPhraseQueryBuilder Cc = QueryBuilders.matchPhraseQuery("Cc", content);
        MatchPhraseQueryBuilder html = QueryBuilders.matchPhraseQuery("html", content);
        MatchPhraseQueryBuilder subject = QueryBuilders.matchPhraseQuery("subject", content);
        MatchPhraseQueryBuilder from = QueryBuilders.matchPhraseQuery("from", content);

        MatchPhraseQueryBuilder pinyintitle = QueryBuilders.matchPhraseQuery("emailAddress.pinyin", content);
        MatchPhraseQueryBuilder pinyinuserName = QueryBuilders.matchPhraseQuery("userName.pinyin", content);
        MatchPhraseQueryBuilder pinyinTo = QueryBuilders.matchPhraseQuery("To.pinyin", content);
        MatchPhraseQueryBuilder pinyinCc = QueryBuilders.matchPhraseQuery("Cc.pinyin", content);
        MatchPhraseQueryBuilder pinyinsubject = QueryBuilders.matchPhraseQuery("subject.pinyin", content);
        MatchPhraseQueryBuilder pinyinhtml = QueryBuilders.matchPhraseQuery("html.pinyin", content);
        MatchPhraseQueryBuilder pinyinfrom = QueryBuilders.matchPhraseQuery("from.pinyin", content);


        disMaxQueryBuilder.add(userName);
        disMaxQueryBuilder.add(Cc);
        disMaxQueryBuilder.add(To);
        disMaxQueryBuilder.add(subject);
        disMaxQueryBuilder.add(html);
        disMaxQueryBuilder.add(title);
        disMaxQueryBuilder.add(from);

        disMaxQueryBuilder.add(pinyinsubject);
        disMaxQueryBuilder.add(pinyinCc);
        disMaxQueryBuilder.add(pinyinhtml);
        disMaxQueryBuilder.add(pinyinuserName);
        disMaxQueryBuilder.add(pinyinTo);
        disMaxQueryBuilder.add(pinyintitle);
        disMaxQueryBuilder.add(pinyinfrom);
        return disMaxQueryBuilder;
    }

}
